<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="bannerList" style="height: 100%; width: 100%;">

</table>

<div id="bannerListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="bannerList_delete()">删除</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
		onclick="bannerList_append()">添加</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="bannerList_edit()">编辑</a> | <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="bannerList_undo()">取消</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="bannerList_accept()">保存</a>| <input id="bannerList_StatusQue"
		style="width: 100px;" /> |<input id="bannerTitleQue"
		style="width: 200px;" />
</div>

<script type="text/javascript">
	function bannerList_delete() {
		var bannerSelect = bannerListDatagrid.datagrid('getSelected');
		if (bannerSelect) {
			$.messager.confirm('确认对话框', '确认删除?', function(r) {
				if (r) {
					$.ajax({
						data : {
							id : bannerSelect.id
						},
						async : false,
						type : 'POST',
						url : baseUrl + '/banner/delete',
						success : function(data) {
							if (data.responseCode == '000') {
								$.messager.alert('提示', '删除成功', 'info');
								bannerListDatagrid.datagrid('deleteRow',
										bannerListDatagrid.datagrid(
												'getRowIndex', bannerSelect));
							} else {
								$.messager.alert('提示', data.responseMessage,
										'error');
							}
						}
					});
				}
			});
		}
	}

	function bannerListDatagridReload() {
		try {
			var queryParams = {
				isEntity : true,
				like_title : bannerTitleQue.textbox('getValue'),
				status : bannerList_StatusQue.combobox('getValue')
			};
			bannerListDatagrid.datagrid("options").queryParams = queryParams;
			bannerListDatagrid.datagrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var bannerList_StatusQue = undefined;
	var bannerTitleQue = undefined;
	var bannerSelectIndex = undefined;
	var bannerSelectRow = undefined;
	var bannerEditIndex = undefined;
	var bannerEditRow = undefined;
	var bannerListDatagrid = undefined;
	$(function() {
		bannerList_StatusQue = $('#bannerList_StatusQue').combobox({
			data : [ {
				"statusCode" : 0,
				"statusMsg" : "全部状态",
				"selected" : true
			}, {
				"statusCode" : 1,
				"statusMsg" : "已发布"
			}, {
				"statusCode" : 2,
				"statusMsg" : "已下线"
			}, {
				"statusCode" : 3,
				"statusMsg" : "待发布"
			} ],
			valueField : 'statusCode',
			textField : 'statusMsg',
			onChange : function(newValue, oldValue) {
				bannerListDatagridReload();
			}
		});

		bannerTitleQue = $('#bannerTitleQue').textbox({
			buttonText : '标题搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		bannerTitleQue.textbox('button').click(function() {
			bannerListDatagridReload();
		});

		bannerListDatagrid = $('#bannerList')
				.datagrid(
						{
							url : '${baseUrl}/banner/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								isEntity : true
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#bannerListToolBars',
							// 							rowStyler : function(index, row) {
							// 								if (row.statusCode == 1) {
							// 									return 'background-color:#B3EE3A;color:#FCFCFC;';
							// 								} else if (row.statusCode == 2) {
							// 									return 'background-color:#F0E68C;color:#FCFCFC;';
							// 								} else if (row.statusCode == 3) {
							// 									return 'background-color:#E8E8E8;color:#FCFCFC;';
							// 								}
							// 							},
							onLoadSuccess : function() {
								bannerSelectIndex = undefined;
								bannerSelectRow = undefined;
								bannerEditIndex = undefined;
								bannerEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (bannerEditIndex != undefined) {
									return false;
								} else {
									bannerSelectIndex = index;
									bannerSelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (bannerEditIndex == undefined) {
									bannerEditIndex = index;
									bannerEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
								var _ed = $(this).datagrid('getEditor', {
									index : index,
									field : 'statusCode'
								});
								row.statusMsg = $(_ed.target).combobox(
										'getText');

								var _platformCodeEd = $(this).datagrid(
										'getEditor', {
											index : index,
											field : 'platformCode'
										});
								row.platformMsg = $(_platformCodeEd.target)
										.combobox('getText');
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.coverUrl) {
												var img = '<img src="'
														+ row.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'platformCode',
										title : '推荐平台',
										width : 120,
										align : 'center',
										formatter : function(value, row, index) {
											return row.platformMsg;
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'platformCode',
												textField : 'platformMsg',
												data : [
														{
															"platformCode" : 1,
															"platformMsg" : "所有平台"
														},
														{
															"platformCode" : 2,
															"platformMsg" : "web和wap"
														},
														{
															"platformCode" : 3,
															"platformMsg" : "android和ios"
														},
														{
															"platformCode" : 4,
															"platformMsg" : "web"
														},
														{
															"platformCode" : 5,
															"platformMsg" : "wap"
														},
														{
															"platformCode" : 6,
															"platformMsg" : "android"
														},
														{
															"platformCode" : 7,
															"platformMsg" : "ios"
														} ],
												required : true
											}
										}
									},
									{
										field : 'url',
										title : '外链',
										width : 270,
										align : 'center',
										editor : {
											type : 'textarea'
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 60,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'startTime',
										title : '开始时间',
										width : 100,
										align : 'left',
										editor : {
											type : 'datetimebox',
											options : {
												required : true
											}
										}
									},
									{
										field : 'endTime',
										title : '结束时间',
										width : 100,
										align : 'left',
										editor : {
											type : 'datetimebox',
											options : {
												required : true
											}
										}
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'title',
										title : '标题',
										width : 249,
										editor : {
											type : 'textarea'
										}
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 1,
													statusMsg : '已发布'
												}, {
													statusCode : 2,
													statusMsg : '已下线'
												}, {
													statusCode : 3,
													statusMsg : '待发布'
												} ],
												required : true
											}
										}
									} ] ],
							onDblClickRow : function(index, row) {
								$.ajax({
									data : {
										id : row.id
									},
									async : false,
									url : baseUrl + '/banner/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		bannerListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							bannerListDatagrid.datagrid('gotoPage',
									bannerListDatagrid.datagrid('getPager')
											.find('.pagination-num').val());
						}
					} ]
				});

	});

	function bannerList_append() {

		$.ajax({
			url : baseUrl + '/banner/add.html',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function bannerList_edit() {

		var bannerSelect = bannerListDatagrid.datagrid('getSelected');
		if (bannerEditIndex != undefined
				&& bannerEditIndex != bannerSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (bannerSelect) {
			if (bannerSelectIndex == undefined) {
				bannerSelectIndex = bannerListDatagrid.datagrid("getRowIndex",
						bannerSelect);
			}

			bannerListDatagrid.datagrid('beginEdit', bannerSelectIndex);
			bannerSelectRow = bannerSelect;

			// 			var _edstartTime = bannerListDatagrid.datagrid('getEditor', {
			// 				index : bannerSelectIndex,
			// 				field : 'startTime'
			// 			});

			// 			console.info(_edstartTime.oldHtml);
			// 			$(_edstartTime.target).datebox('setValue', _edstartTime.oldHtml);

		}
	}

	function bannerList_undo() {
		if (bannerSelectIndex != undefined) {
			bannerListDatagrid.datagrid('rejectChanges');
			bannerSelectIndex = undefined;
			bannerSelectRow = undefined;
			bannerEditIndex = undefined;
			bannerEditRow = undefined;
		}
	}

	function bannerList_accept() {
		if (bannerSelectIndex != undefined) {
			if (bannerListDatagrid.datagrid('validateRow', bannerSelectIndex)) {

				//验证发布状态
				var _edStatusCode = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'statusCode'
				});
				var _statusMsg = $(_edStatusCode.target).combobox('getText');
				if (_statusMsg != '已发布' && _statusMsg != '待发布'
						&& _statusMsg != '已下线') {
					$.messager.alert('提示', '未知状态', 'warning');
					return;
				}

				//验证推荐平台
				var _edPlatformCode = bannerListDatagrid.datagrid('getEditor',
						{
							index : bannerSelectIndex,
							field : 'platformCode'
						});
				var _PlatformMsg = $(_edPlatformCode.target)
						.combobox('getText');

				if (_PlatformMsg != '所有平台' && _PlatformMsg != 'web和wap'
						&& _PlatformMsg != 'android和ios'
						&& _PlatformMsg != 'web' && _PlatformMsg != 'wap'
						&& _PlatformMsg != 'android' && _PlatformMsg != 'ios') {
					$.messager.alert('提示', '未知平台', 'warning');
					return;
				}

				//验证开始时间
				var _edstartTime = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'startTime'
				});
				var _startTime = $(_edstartTime.target).datetimebox('getValue');

				if (!_startTime) {
					$.messager.alert('提示', '开始时间不能为空', 'warning');
					return;
				}

				//验证结束时间
				var _edendTime = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'endTime'
				});
				var _endTime = $(_edendTime.target).datetimebox('getValue');
				if (!_endTime) {
					$.messager.alert('提示', '结束时间不能为空', 'warning');
					return;
				}

				//验证标题
				var _edTitle = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'title'
				});

				var _Title = $(_edTitle.target).val();
				if (!_Title || _Title.trim() == '') {
					$.messager.alert('提示', '标题不能为空', 'warning');
					return;
				}

				//获取外链
				var _edUrl = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'url'
				});

				var _Url = $(_edUrl.target).val();

				//获取排序
				var _edSort = bannerListDatagrid.datagrid('getEditor', {
					index : bannerSelectIndex,
					field : 'sort'
				});

				var _Sort = $(_edSort.target).val();

				var _Data = {
					id : bannerSelectRow.id,
					title : _Title,
					sort : _Sort,
					startTimeStr : _startTime,
					endTimeStr : _endTime,
					url : _Url,
					status : $(_edStatusCode.target).combobox('getValue'),
					platform : $(_edPlatformCode.target).combobox('getValue')
				};

				$.ajax({
					data : _Data,
					async : false,
					type : 'POST',
					url : baseUrl + '/banner/update',
					success : function(data) {
						if (data.responseCode == '000') {
							bannerListDatagrid.datagrid('endEdit',
									bannerSelectIndex);

							bannerListDatagrid.datagrid('acceptChanges');

							bannerSelectIndex = undefined;
							bannerSelectRow = undefined;
							bannerEditIndex = undefined;
							bannerEditRow = undefined;

							$.messager.alert('提示', '修改成功', 'info');
						} else {
							$.messager.alert('提示', data.responseMessage,
									'error');
						}
					}
				});

			}
		}
	}
</script>


<script type="text/javascript">
	function setHeight() {
		//				var c = $('#cc');
		//				var p = c.layout('panel', 'center'); // get the center panel
		//				var oldHeight = p.panel('panel').outerHeight();
		//				p.panel('resize', {
		//					height: 'auto'
		//				});
		//				var newHeight = p.panel('panel').outerHeight();
		//				c.layout('resize', {
		//					height: (c.height() + newHeight - oldHeight)
		//				});
	}
</script>

